/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Data.Type;
using Core.Model.Data.Generics;
using System.IO;
using System.Drawing;
using Core.Model.Events;
using Core.Model.Data;
using Engine;

namespace Core.Model.Ingredients
{
    public class Ingredient : Root, ICloneable
    {

        #region Exchanged Attributes

        private bool initiallyActive = true;

        public bool InitiallyActive
        {
            get { return initiallyActive; }
            set { initiallyActive = value; }
        }
        private int? contentHook = null;

        public int? ContentHook
        {
            get { return contentHook; }
            set { contentHook = value; }
        }
        private Content originalContent = null;

        public Content OriginalContent
        {
            get { return originalContent; }
            set { originalContent = value; }
        }
        private bool shared = false;

        public bool Shared
        {
            get { return shared; }
            set { shared = value; }
        }

        #endregion

        #region Internal Attributes

        private Content content = null;

        public Content Content
        {
            get { return content; }
            set { content = value; }
        }

        public override void InittInternalAttributes ()
        {
            base.InittInternalAttributes();

            if ( OriginalContent != null )
            {
                
                if ( OriginalContent.IsReference() )
                {
                    Content = new Content();

                    Engine.MHEGEngine.Instance.SetContent( OriginalContent.Reference, Content, ContentHook.Value );

                    // SetData( OriginalContent.Reference );
                }
                else
                    Content = OriginalContent.Clone();

            }
        }

        #endregion

        #region Internal Behaviours

        public override void Destroy ()
        {
            if ( !AvailabilityStatus )
                return;

            if ( Content != null &&
                Content.ContentCachePriority.HasValue && Content.ContentCachePriority == 0 )
                Content = null;
            else
                Content = null; //todo cache

            base.Destroy();
        }

        #endregion

        #region Actions

        public virtual void SetData ( GenericOctetString newContent )
        {
            if ( newContent == null )
            {
                log.Error( "SetData: new included content is null." );
                throw new NullReferenceException();
                return;
            }

            if ( Content == null )
                Content = new Content();

            //if ( ContentHook == Engine.MHEGEngine.Instance.ActiveApplication.TextContentHook )
            //    Content.TextContent = newContent.Value;
            //else if ( Util.DefaultContentHooks.IsBitmapContentHool( ContentHook.Value ) )
            //{
            //    log.Warn( "SetData: Trying to set bitmap with included data ("+newContent.Value+"). Trying to use as reference" );
            //    SetData( new GenericContentRef( new ContentRef( newContent.Value ) ) );
            //}

        }

        public virtual void SetData ( GenericContentRef newContent )
        {
            if ( newContent == null )
            {
                log.Error( "SetData: new referenced content is null." );
                throw new NullReferenceException();
                return;
            }

            if ( Content == null )
                Content = new Content();

            Engine.MHEGEngine.Instance.SetContent( newContent, Content, ContentHook.Value );


        }

        public void Preload ()
        {
            Prepare();
            //hint to retrive data
        }

        public void Unload ()
        {
            Destroy();
            //hint to free resources
        }

        #endregion

        #region ICloneable Members

        public object Clone ()
        {
            return this;
        }

        #endregion
    }
}
